from django.shortcuts import render
from users.models import User

# Create users views here.
# 列表
def home(request):
    """ 显示首页 """
    return render(request, 'index.html')


def user_list(request):
    """ 展示所有用户列表 """
    users = User.objects.all()

    context = {'users': users}

    return render(request, 'users/user-list.html', context=context)


def user_detail(request, user_id):
    """ 显示单个用户信息 """
    user = User.objects.get(pk=user_id)

    print(user)

    context = {'user': user}

    return render(request, 'users/user-detail.html', context=context)


def user_find(request, user_id, gender):
    """ 传入关键信息查找用户 """

    if gender == 'male':
        gender =  True
    elif gender == 'female':
        gender =  False

    # filter查询的结果是一个QuerySet查询集，要通过first来取出第一个。
    user = User.objects.filter(pk=user_id, gender=gender).first()

    return render(request, 'users/user-find.html', context={'user': user})
